package cn.itcast.dao.Impl;

import cn.itcast.dao.studentDao;
import cn.itcast.domain.Student;

import java.io.*;

/**
 * @param
 * @return
 */
public class studentDaoImpl implements studentDao {
    @Override
    public boolean login(String username, String password) {
        //定义返回值
        boolean falg = false;
        try {
            //使用字符流将硬盘文件中存储的用户信息读取到内存中
            FileInputStream fis = new FileInputStream("d://user.txt");
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            //定义空字符串接受读取结果
            String len = null;
            while ((len = br.readLine()) != null) {
                //读取到的字符串按照逗号切割
                String[] split = len.split(",");
                //判断用户名和密码是否均和txt文件中一致
                if (username.equals(split[0]) && password.equals(split[1])) {
                    //只要有一组是一致的就表示验证通过，就可以结束循环了
                    falg = true;
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return falg;
    }

    @Override
    public void add(Student student) {
        try {
            //通过字符输出流，将对象中封装的信息写入到硬盘中的stu.txt文件中
            BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\stu.txt",true));//续写而不覆盖
            bw.write(student.getName()+","+student.getAge()+","+student.getScore());
            bw.newLine();//换行关流
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
